package util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordReader implements Runnable {
	private String activeXParam;
	private String propertyParam;
	private String inputFIle;
	private String type;

	public WordReader(String inputFIle, String type) {
		this.inputFIle = inputFIle;
		this.type = type;
	}

	private void extractDoc() {
		try {
			if (type.equals("txt")) {
				Runtime runtime = Runtime.getRuntime();
				runtime.exec("notepad " + inputFIle);
				return;
			} else if (type.equals("docx") || type.equals("doc")) {
				activeXParam = "Word.Application";
				propertyParam = "Documents";
			} else if (type.equals("xls") || type.equals("xlsx")) {
				activeXParam = "Excel.Application";
				propertyParam = "Workbooks";
			} else if (type.equals("ppt") || type.equals("pptx")) {
				activeXParam = "Powerpoint.Application";
				propertyParam = "Presentations";
			}
			// Excel.Application Word.Application Powerpoint.Application
			ActiveXComponent app = new ActiveXComponent(activeXParam);
			// 设置可见
			app.setProperty("Visible", new Variant(true));
			// 打开word文件 Documents Workbooks Presentations
			Dispatch doc1 = app.getProperty(propertyParam).toDispatch();
			Dispatch.invoke(
					doc1,
					"Open",
					Dispatch.Method,
					new Object[] { inputFIle, new Variant(false),
							new Variant(true) }, new int[1]).toDispatch();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		extractDoc();
	}

	// public static void main(String[] args) {
	// //
	// WordReader.extractDoc("C:/Users/wj/Desktop/论文翻译.docx","C:/Users/wj/Desktop/论文翻译.txt");
	// try {
	// WordReader.extractDoc("C:/Users/wj/Desktop/答题.txt","txt");
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
}
